package com.app.car.check.info;

import android.os.Build;
import android.os.Bundle;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.app.car.R;
import com.app.car.basis.BasisActivity;
import com.app.car.basis.MyViewPagerAdapter;
import com.app.car.bean.RfidListBean;
import com.app.car.greendao.GreenDaoUtils;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.tab.QMUITabBuilder;
import com.qmuiteam.qmui.widget.tab.QMUITabIndicator;
import com.qmuiteam.qmui.widget.tab.QMUITabSegment;

import butterknife.BindView;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableEmitter;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

/**
 * 盘点明细
 */
public class CheckTabActivity extends BasisActivity {


    @BindView(R.id.tabSegment)
    QMUITabSegment mTabSegment;
    @BindView(R.id.viewpager)
    ViewPager viewPager;
    MyViewPagerAdapter myViewPagerAdapter;
    long id;
    RfidListBean listBean;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void initViews() {
        setContentView(R.layout.check_tab_activity);
        setTitleBg(R.color.base_text_green);
        setTitle("盘点明细");
        setTitleTextColor(R.color.white);
        setTitleLeftButton(null);

    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
//        QMUIStatusBarHelper.translucent(mContext);
        id =  getIntent().getLongExtra("id",0);
        if(id == 0){
            showToast(R.string.app_data_empty);
            return;
        }

        setView();

    }


    private void setView() {

        showLoading("刷新中！");

        Observable.create(new ObservableOnSubscribe<Integer>() {
            // 1\. 创建被观察者 & 生产事件
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                listBean = GreenDaoUtils.getRfidListBeanById(id);
                emitter.onNext(1);
                emitter.onComplete();
            }
        })   .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Integer>() {
                    // 2\. 通过通过订阅（subscribe）连接观察者和被观察者
                    // 3\. 创建观察者 & 定义响应事件的行为
                    @Override
                    public void onSubscribe(Disposable d) {

                    }
                    // 默认最先调用复写的 onSubscribe（）

                    @Override
                    public void onNext(Integer value) {
                        dismissProgress();
                        String[] title = {
                                "已盘点:"+listBean.getCheckRfidList().size(),
                                "盘到:"+listBean.getPanDaoList().size(),
                                "盘亏:"+listBean.getPanKuiList().size(),
                                "盘盈:"+listBean.getPanYingList().size()
                        };
                        Fragment[] fms = new Fragment[4];
                        fms[0]= CheckInfoFragment.newInstance(listBean.getCheckRfidList());
                        fms[1]= CheckInfoFragment.newInstance(listBean.getPanDaoList());
                        fms[2]= CheckInfoFragment.newInstance(listBean.getPanKuiList(),listBean.getTime(),listBean.getBookRfidList());
                        fms[3]= CheckInfoFragment.newInstance(listBean.getPanYingList());

                        myViewPagerAdapter = new MyViewPagerAdapter(getSupportFragmentManager(), fms);
                        viewPager.setAdapter(myViewPagerAdapter);
                        viewPager.setOffscreenPageLimit(3);


                        QMUITabBuilder tabBuilder = mTabSegment.tabBuilder();
                        for (int i = 0; i < title.length; i++) {
                            mTabSegment.addTab(tabBuilder.setText(title[i]).build(mContext));
                        }
                        int space = QMUIDisplayHelper.dp2px(mContext, 16);
                        mTabSegment.setIndicator(new QMUITabIndicator(
                                QMUIDisplayHelper.dp2px(mContext, 2), false, true));
                        mTabSegment.setMode(QMUITabSegment.MODE_SCROLLABLE);
                        mTabSegment.setItemSpaceInScrollMode(space);
                        mTabSegment.setupWithViewPager(viewPager, false);
                        mTabSegment.setPadding(space, 0, space, 0);
                        mTabSegment.addOnTabSelectedListener(new QMUITabSegment.OnTabSelectedListener() {
                            @Override
                            public void onTabSelected(int index) {
                            }

                            @Override
                            public void onTabUnselected(int index) {
                            }

                            @Override
                            public void onTabReselected(int index) {
                            }

                            @Override
                            public void onDoubleTap(int index) {
                            }
                        });

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }




}